﻿using Dapper;
using System;
using System.Data;
using ZhongKeSite.Common.Helper;

namespace ZhongKeSite.Repository.Dapper
{
    public partial class Dapper
    {
        /// <summary>
        /// 一审
        /// </summary>
        /// <returns></returns>
        public bool FirstApproval(string orderNo, string remark, int status, string username)
        {
            try
            {
                string sql = string.Empty;
                if (status == 1)
                {
                    //审核通过
                    sql = @"update Orders set Status=2,ApprovalBy1=@ApprovalBy1,ApprovalDate1=GETDATE(),ApprovalRemark1=@ApprovalRemark1 where OrderNo=@OrderNo ; ";
                }
                else
                {
                    //审核不通过
                    sql = @"update Orders set Status=8,ApprovalBy1=@ApprovalBy1,ApprovalDate1=GETDATE(),ApprovalRemark1=@ApprovalRemark1 where OrderNo=@OrderNo ; ";
                    sql += @"update OrdersTransfer set Status=2 where Status=0 and OrderNo=@OrderNo ; ";
                }

                using (IDbConnection connection = GetSqlConnection())
                {
                    return connection.Execute(sql, new
                    {
                        ApprovalBy1 = username,
                        ApprovalRemark1 = remark,
                        OrderNo = orderNo
                    }) >0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 二审
        /// </summary>
        /// <returns></returns>
        public bool SecondApproval(string orderNo, string remark, int status, string username)
        {
            try
            {
                string sql = string.Empty;
                if (status == 1)
                {
                    //审核通过
                    sql = @"update Orders set Status=3,ApprovalBy2=@ApprovalBy2,ApprovalDate2=GETDATE(),ApprovalRemark2=@ApprovalRemark2 where OrderNo=@OrderNo ; ";
                }
                else
                {
                    //审核不通过
                    sql = @"update Orders set Status=8,ApprovalBy2=@ApprovalBy2,ApprovalDate2=GETDATE(),ApprovalRemark2=@ApprovalRemark2 where OrderNo=@OrderNo ; ";
                    sql += @"update OrdersTransfer set Status=2 where Status=0 and OrderNo=@OrderNo ; ";
                }

                using (IDbConnection connection = GetSqlConnection())
                {
                    return connection.Execute(sql, new
                    {
                        ApprovalBy2 = username,
                        ApprovalRemark2 = remark,
                        OrderNo = orderNo
                    }) > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                return false;
            }
        }
    }
}
